<?php
class omeauto_condition_salesplatform extends omeauto_auto{
    public $name = '来源销售平台';
    
    /*
     * 
     * 设置获取主html时的一些自定义smarty变量
     */
    function pagedata($data){
        $pagedata = array();
        
        $ome_shop_type = ome_shop_type::get_shop_type();
        $shop_type = array();
        $i = 0;
        if ($ome_shop_type)
        foreach ($ome_shop_type as $k=>$v){
        	$shop_type[$i] = array('key'=>$k,'label'=>$v);
        	$i++;
        }
        
        if($data){
            if($data['shop_type']){
                $pagedata['current_shop_type'] = $data['shop_type'];
                $checked = $data['shop'];
                $shop = $this->_get_shop($data['shop_type'],$checked);
                $pagedata['shop'] = $shop;
            }
        }else{
            $shop = $this->_get_shop($shop_type[0]['key'],$checked);
            $pagedata['shop'] = $shop;
        }
        
        $uniqid = uniqid();
        
        
        $pagedata['shop_type'] = $shop_type;
        $pagedata['random_id'] = $uniqid;
        $pagedata['shop_area'] = $uniqid."_shop";
        
        return $pagedata;
    }
    
    /*
     * 获取条件的详情
     */
    function condition_detail($data){
        return parent::condition_detail($data);
    }
    
    /*
     * 下拉详情
     */
    function detail_basic($data){
        $render = $this->get_render();
        
    	$ome_shop_type = ome_shop_type::get_shop_type();
        $shop_type = array();
        $i = 0;
        if ($ome_shop_type)
        foreach ($ome_shop_type as $k=>$v){
        	$shop_type[$i] = array('key'=>$k,'label'=>$v);
        	$i++;
        }
        
        foreach($shop_type as $v){
            if($v['key'] == $data['shop_type']){
                $shop_type = $v['label'];
                break;
            }
        }
        
        $shop = array();
        
        if($data['shop']){
            $oShop = &app::get('ome')->model('shop');
            foreach($data['shop'] as $v){
                $shop_info = $oShop->dump($v,'name');
                $shop[] = $shop_info['name'];
            }
        }
        
        $render->pagedata['shop_type'] = $shop_type;
        $render->pagedata['shop'] = implode("、",$shop);
        
        return $render->fetch("condition/salesplatform/detail_basic.html");
    }
    
    /*
     * 验证订单的sdf数据条件是否通过
     * 
     */
    function validate($order_sdf,$condition_data){
        $shop_id = $order_sdf['shop_id'];
        $shop_type = $order_sdf['shop_type'];
        if(!empty($condition_data['shop'])){
            if(in_array($shop_id,$condition_data['shop'])){
                return true;
            }else{
                return false;
            }
        }else{
            if($shop_type == $condition_data['shop_type']){
                return true;
            }else{
                return false;
            }
        }
    }
    
    /*********************************************以下是非必须函数，多为过程函数*******************************************************/
    
    
    function _get_shop($shop_type,$checked=array()){
        $shop = array();
        if($shop_type){
            $rows = app::get('ome')->model('shop')->getList("shop_id,name",array("shop_type"=>$shop_type),0,-1);
            if($rows){
                foreach($rows as $v){
                    $shop[] = array(
                        'shop_id' => $v['shop_id'],
                        'shop_name' => $v['name'],
                        'checked' => ($checked && in_array($v['shop_id'],$checked))?'checked':'', 
                    );
                }
            }
        }
        return $shop;
    }
    
    function get_shop_by_type($key,$shop_type){
        $render = $this->get_render();
        $shop = $this->_get_shop($shop_type);
        $render->pagedata['current_shop_type'] = $shop_type;
        $render->pagedata['shop'] = $shop;
        $render->pagedata['KEY'] = $key;
        echo $render->fetch("condition/salesplatform/shop.html");
    }
}